#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <Python.h>
#include "changesettings.h"
#include "magnifierwindow.h"
#include "showtextwindow.h"
#include "texttospeech.h"
#include "cutimage.h"
#include <QMainWindow>
#include <QDir>
#include <QPushButton>
#include <QListWidget>
#include <QListWidgetItem>
#include <QKeyEvent>
#include <QLabel>
#include <QEvent>
#include <QThreadPool>
#include <QRunnable>
#include <QTimer>
#include <QProcess>
#include <QVector>
#include <QCursor>
#include <QHotkey>
#include <QPainter>
#include <QLinearGradient>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QTime>


#define TITLE "麒麟颂语"

/*class TextToSpeechTask : public QRunnable
{
public:
    TextToSpeechTask(const QString& text);

    void run() override;

private:
    QString m_text;
};*/

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void importPythonModules();

    void importPythonFunctions();

    void installFilter();

    void microsoftSpeak();

    void googleSpeak(QString text);

    void pyttsSpeak(QString text);

    void speakText(QString text);

    void textChange(QString text);
    //style beautify
    void playAudio();

    void showTextBox();

    void menuIcon();

    void windowBeautify();

    void connectSlots();

    //pythonAPI
    void setPyttsx3ComboBox();

    void generateAudio(QString text, QString path);

    void initializeWindow();


private slots:
    void on_menuList_currentRowChanged(int currentRow);

    //void onmenulistItem(QListWidgetItem *item);

    void menulistItemChanged(QListWidgetItem *current, QListWidgetItem *previous);

    void on_btnCancel_clicked();

    void on_btnStart_clicked();

    void updateratevalue(int);

    void updatepitchvalue(int);

    void updatezoomvalue(int);

    void onvoicechange(int value);

    //tts
    void setRate(int);

    void setPitch(int);

    //two timer
    void mouseInText();

    void handleImgChange();

    void magnifierWindowSlot();  //放大镜

    void normalizeWindow(); //窗口正常化

    void hotkeyStart();  //开始使用

    void hotkeyQuit();  //退出使用

    void showTextWindowSlots();   //显示文本框

    void preHandle();

    //global style
    void onComboBoxIndexChanged(int);

    void on_setPlayVisible_clicked();

    void on_setPlayInvisible_clicked();

    void on_setSmallFont_clicked();

    void on_setMiddleFont_clicked();

    void on_setBigFont_clicked();

    void on_generateButton_clicked();

    void on_changeCursor_clicked();

    void on_recoverCursor_clicked();

    void on_showTextArea_clicked();

    void on_showTextCancel_clicked();

    void checkSlot1(bool flag);
    void checkSlot2(bool flag);
    void checkSlot3(bool flag);

    void on_generateButton_listen_clicked();

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;

    void mousePressEvent(QMouseEvent *event) override;

    void mouseMoveEvent(QMouseEvent *event) override;

    void paintEvent(QPaintEvent *event) override;

private:
    Ui::MainWindow *ui;
    QPoint lastMousePosition;
    int nowTheme;
    //timer
    QTimer* posTimer;
    QTimer* ocrTimer;
    QTimer* preHandleTimer;
    //cutimage
    CutImage *cutimage;
    double tolerance;
    QPoint nowPos;
    //ocr
    PyObject* pModule1;
    PyObject* pModule2;
    PyObject* pDeteImport;
    PyObject* pRecoImport;
    PyObject* pDeteOcr;
    PyObject* pRecoOcr;
    //tts
    PyObject* tts;
    PyObject* pyttsx3;
    PyObject* gtts;
    PyObject* speak_microsoft;
    PyObject* speak_gtts;
    PyObject* speak_pyttsx3;
    PyObject* changeText;
    PyObject* changeRate;
    PyObject* changePitch;
    PyObject* changeVoice;
    PyObject* setPyttsx3;
    PyObject* generateMp3;
    //放大镜
    qreal zoom;
    MagnifierWindow* magnifier; // 放大镜窗口对象
    bool isMagnifierEnabled;   // 放大镜是否已经开启
    //文本框
    ShowTextWindow* textwindow; // 文本框窗口对象
    bool isTextWindowEnabled;
    //QHotkeys
    QHotkey* magnifierKey;
    QHotkey* normalizeKey;
    QHotkey* startKey;
    QHotkey* quitKey;
    QHotkey* showtextfierKey;
    //字体大小
    int fontSize;
    //是否开启语音提示
    int enableAudioTips;
    QVector<int> temptVector;
    QVector<int> textVector;
    //播放音频
    QMediaPlayer* mediaPlayer;
    QString audioPath;
    //the mode of tts
    int ttsMode;
};
#endif // MAINWINDOW_H
